package com.ynkbny.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynkbny.base.PageResult;
import com.ynkbny.domain.RouterLow;
import com.ynkbny.domain.pojo.RoleWithRouter;
import com.ynkbny.mapper.RouterMapper;
import com.ynkbny.service.MenuService;
import com.ynkbny.util.SearchUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author zelin.wang
 * @description 菜单service实现类
 * @date 9/4/2023
 */
@Slf4j
@Service
public class MenuServiceImpl extends ServiceImpl<RouterMapper, RouterLow>
        implements MenuService {

    @Resource
    RoleAndRouterServiceImpl roleAndRouterService;

    @Override
    public PageResult<RouterLow> pageList(String conditionJson, int pageNum, int pageSize) {
        Page<RouterLow> page = new Page<>(pageNum, pageSize);
        QueryWrapper<RouterLow> queryWrapper = SearchUtil.parseWhereSql(null, null, conditionJson);
        return new PageResult<>(this.baseMapper.selectPage(page, queryWrapper));
    }

    @Override
    public Boolean roleBind(List<RoleWithRouter> ids) {
        log.info("开始更改权限");
        int roleId = ids.get(0).getRoleId();
        LambdaQueryWrapper<RoleWithRouter> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleWithRouter::getRoleId,ids.get(0).getRoleId());
        try{
            roleAndRouterService.getBaseMapper().delete(wrapper);
            Collection<RoleWithRouter> data = new ArrayList<>(ids);
            roleAndRouterService.saveBatch(data);
            log.info("更改权限成功");
            return true;
        }catch (Exception e){
            log.error("更新权限失败,错误信息为:"+e.getMessage());
            return false;
        }
    }

}
